package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Recruit;
import net.zjitc.entity.VO.RecruitVo;
import net.zjitc.service.IRecruitService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-05-05 14:07:31
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "招聘信息管理")
public class RecruitController {
    @Reference
    private IRecruitService recruitService;

    ResponseResult result=new ResponseResult();

    @ApiOperation("获取所有招聘信息")
    @GetMapping("recruit")
    public ResponseResult getRecruits(@RequestParam(required = false,value ="querystring" ) String querystring,
                                      @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                      @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<RecruitVo> recruit=recruitService.getRecruit(querystring,pagenum,pagesize);
        if (recruit.getTotal()>0){
            result.setSuccess("获取成功",recruit);
        }else {
            result.setError("获取失败");
        }
        return result;
    }
    @ApiOperation("根据id查找招聘信息")
    @GetMapping("recruit/{id}")
    public ResponseResult getRecruitById(@PathVariable("id") Integer id){

        RecruitVo recruit =recruitService.selectById(id);
        if (recruit!=null){
            result.setSuccess("获取招聘信息成功",recruit);
        }else {
            result.setError("获取招聘信息失败");
        }
        return result;
    }
    @ApiOperation("根据comid查找招聘信息")
    @GetMapping("getRecruitByComId/{id}")
    public ResponseResult getRecruitByComId(@PathVariable("id") Integer id,
                                            @RequestParam(required = false,value ="querystring" ) String querystring,
                                            @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                            @RequestParam(required = true,value = "pagesize") Integer pagesize){

        Page<RecruitVo> recruit =recruitService.getRecruitByComId(id,querystring,pagenum,pagesize);
        if (recruit.getTotal()>0){
            result.setSuccess("获取招聘信息成功",recruit);
        }else {
            result.setError("获取招聘信息失败");
        }
        return result;
    }
    @ApiOperation("添加招聘信息")
    @PostMapping("AddRecruit/{id}")
    public ResponseResult AddRecruit(@PathVariable("id") Integer id,
                                     @RequestBody Recruit recruit){
        recruit.setComid(id);
        String[] cateids = recruit.getCateids().split(",");
        recruit.setCateid(Integer.parseInt(cateids[2]));
        String[] placeids = recruit.getPlaceids().split(",");
        recruit.setPlaceid(Integer.parseInt(placeids[2]));
        Date time=new Date();
        recruit.setAddtime(time);
        boolean save = recruitService.save(recruit);
        if (save){
            result.setCreateSuccess("添加招聘信息成功");
        }else {
            result.setError("添加招聘信息失败");
        }
        return result;
    }
    @ApiOperation("修改招聘信息")
    @PutMapping("recruit/{id}")
    public ResponseResult UpdateRecruit(@RequestBody Recruit recruit,@PathVariable("id") int id){
        String[] cateids = recruit.getCateids().split(",");
        recruit.setCateid(Integer.parseInt(cateids[2]));
        String[] placeids = recruit.getPlaceids().split(",");
        recruit.setPlaceid(Integer.parseInt(placeids[2]));
        recruit.setLegal("0");
        boolean update = recruitService.updateById(recruit);
        if (update){
            result.setSuccess("修改招聘信息成功");
        }else {
            result.setError("修改招聘信息失败");
        }
        return result;
    }
    @ApiOperation("修改招聘信息")
    @PutMapping("recruitlegal/{id}")
    public ResponseResult UpdateRecruitlegal(@RequestBody Recruit recruit,@PathVariable("id") int id){
        String[] cateids = recruit.getCateids().split(",");
        recruit.setCateid(Integer.parseInt(cateids[2]));
        String[] placeids = recruit.getPlaceids().split(",");
        recruit.setPlaceid(Integer.parseInt(placeids[2]));
        boolean update = recruitService.updateById(recruit);
        if (update){
            result.setSuccess("修改招聘信息成功");
        }else {
            result.setError("修改招聘信息失败");
        }
        return result;
    }
    @ApiOperation("删除招聘信息")
    @DeleteMapping("recruit/{id}")
    public ResponseResult DeleteRecruit(@PathVariable("id") int id){

        int delete= recruitService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除招聘信息成功");
        }else {
            result.setError("删除招聘信息失败");
        }
        return result;
    }
    @ApiOperation("根据条件查询招聘信息")
    @GetMapping("SearchRecruid")
    public ResponseResult SearchRecruid(@RequestParam(required = false) String querystring,
                                        @RequestParam(required = false) Integer eduid,
                                        @RequestParam(required = false) String cateids,
                                        @RequestParam(required = false) String placeids,
                                        @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                        @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<RecruitVo> recruitVoPage= recruitService.SearchRecruid(querystring,eduid,cateids,placeids,pagenum,pagesize);
        if (recruitVoPage.getTotal()!=0){
            result.setSuccess("获取职位成功",recruitVoPage);
        }else {
            result.setError("获取职位失败");
        }
        return result;
    }

    @ApiOperation("根据企业和条件查询招聘信息")
    @GetMapping("SearchRecruidByComId/{id}")
    public ResponseResult SearchRecruidByComId(@RequestParam(required = false) Integer eduid,
                                                @RequestParam(required = false) String cateids,
                                                @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                                @RequestParam(required = true,value = "pagesize") Integer pagesize,
                                                @PathVariable("id") int id){
        Page<RecruitVo> recruitVoPage= recruitService.SearchRecruidByComId(eduid,cateids,pagenum,pagesize,id);
        if (recruitVoPage.getTotal()!=0){
            result.setSuccess("获取职位成功",recruitVoPage);
        }else {
            result.setError("获取职位失败");
        }
        return result;
    }




}

